﻿using System;
using System.Collections.Generic;

namespace Pixels.Logic
{
    public enum Resource
    {
        Apples,
        Cogs,
        Special,
        Power,
        Max,
    };

    public class ResourceConsts
    {
        public readonly Resource ID;
        public readonly string   name;
        public readonly string   icon;
        public Sargon.Color color;

        public ResourceConsts(Resource id, string name, string icon, Sargon.Color color) {
            this.name = name;
            this.ID = id;
            this.icon = icon;
            this.color = color;
        }
    }


    public class ResourcePool
    {
        private int[] _raw;
        public bool AllowNegatives = false;

        public ResourcePool(bool allowNegatives = true) {
            this._raw = new int[(int)Resource.Max];
            this.AllowNegatives = allowNegatives;
        }

        public void changeResource(Resource res, int amount) {
            this._raw[(int)res] += amount;
        }

        public bool hasEnough(ResourcePool cost) {
            for (Resource r = 0; r < Resource.Max; r++) {
                if (this[r] < cost[r]) return false;
            }
            return true;
        }

        public void subtractCost(ResourcePool cost) {
            if (!hasEnough(cost) && !AllowNegatives) throw new Exception("Cannot subtract! Cost too high");
            for (Resource r = 0; r < Resource.Max; r++) {
                changeResource(r, -cost[r]);
            }
        }

        public void addPoolToThisPool(ResourcePool toAdd) {
            for (Resource r = 0; r < Resource.Max; r++) { if (toAdd[r] < 0) throw new Exception("Use this proc. only to add resources, not to subtract!"); changeResource(r, toAdd[r]); } 
        }

        /// <returns>true if there are DEFICITS in the new pool </returns>        
        public bool addPoolToPoolAndCheckDeficits(ResourcePool pool, out ResourcePool deficits) {

            bool hasDeficit = false;
            deficits = new ResourcePool();

            for (Resource r = 0; r < Resource.Max; r++) {
                changeResource(r, pool[r]);
                if (!AllowNegatives && this[r] < 0) {                    
                    deficits.changeResource(r, this[r] );
                    _raw[(int)r] = 0;
                    hasDeficit = true;
                }
            }
            return hasDeficit;
            
        }

        public int this[Resource index]
	    {
            get { return _raw[(int)index]; }		    
	    }

        internal void clear() {
            for (Resource i = 0; i < Resource.Max; i++) _raw[(int)i] = 0;
        }
    }
}
